package com.my.helper.activity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.my.helper.R;
import com.my.helper.adapter.HistoryAdapter;
import com.my.helper.bean.HistoryData;
import com.my.helper.util.CacheActivity;
import com.my.helper.util.MyDataBaseHelper;
import com.my.helper.util.SharedPrefUtil;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

public class HistoryActivity extends AppCompatActivity implements View.OnClickListener {
    private RelativeLayout rlBack;
    private RefreshLayout refreshLayout;
    private GridLayoutManager mGridLayoutManager;
    private RecyclerView mRecyclerView;
    private SharedPrefUtil sp;
    private MyDataBaseHelper mSql;
    private List<HistoryData> datas = new ArrayList<HistoryData>();
    private HistoryAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        CacheActivity.addActivity(this);
        sp = new SharedPrefUtil(this, "myHelper");
        mSql = new MyDataBaseHelper(this, "Helper.db", null, 1);
        initView();
        initListener();
        initData();
    }

    private void initView() {
        rlBack = findViewById(R.id.rl_back_history);
        refreshLayout = findViewById(R.id.refreshLayout_history);
        mRecyclerView = findViewById(R.id.module_rv_history);
        adapter = new HistoryAdapter(this);
        mGridLayoutManager = new GridLayoutManager(this,
                1, GridLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(mGridLayoutManager);
        mRecyclerView.setAdapter(adapter);
    }

    private void initListener() {
        rlBack.setOnClickListener(this);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) { // 下拉刷新
                refreshLayout.finishRefresh(1000, true);
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {// 上拉加载
                refreshLayout.finishLoadMore(1000, true, true);
            }
        });

        adapter.setListener(new HistoryAdapter.OnHistoryClickListener() {
            @Override
            public void onClickListener(int position) {
                goIntent(position);
            }
        });
    }

    private void goIntent(int position) {
        Intent intent = new Intent(HistoryActivity.this, MessageWebViewActivity.class);
        intent.putExtra("web_message_title", datas.get(position).getTitle());
        intent.putExtra("web_message_url", datas.get(position).getUrl());
        intent.putExtra("web_message_type", "历史");
        intent.putExtra("web_message_news_title", datas.get(position).getTitle());
        startActivity(intent);
    }

    private void initData() {
        // 清除数据
        datas.clear();
        int userId = Integer.valueOf(sp.getString("user_id", "0"));
        SQLiteDatabase db = mSql.getWritableDatabase();
        // 查询 MyMemorandum 表中符合 id 的数据
        Cursor cursor = db.query("MyHistory", null, "uid=?",
                new String[]{"" + userId}, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                HistoryData bean = new HistoryData();
                bean.setTitle(cursor.getString(cursor.getColumnIndex("title")));
                bean.setType(cursor.getString(cursor.getColumnIndex("type")));
                bean.setTime(cursor.getString(cursor.getColumnIndex("time")));
                bean.setUrl(cursor.getString(cursor.getColumnIndex("url")));
                datas.add(bean);
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();

        adapter.setDatas(datas);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_back_history: // 返回
                CacheActivity.finishSingleActivityByClass(HistoryActivity.class);
                break;
        }
    }
}
